package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.mine.adapter.SearchGuiderStoreAdapter;
import com.meitianhui.convenitenbuy.module.mine.contract.SearchGuiderStoreContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.presenter.SearchGuiderStorePresenter;
import com.meitianhui.convenitenbuy.module.store.model.StoreInfo;
import com.meitianhui.convenitenbuy.util.TipUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/*
 Created by amy on 2017/11/28.
 */

public class SearchGuiderStoreActivity extends BaseToolbarActivity implements SearchGuiderStoreContract.View {
    private EditText mEtSearchGuiderStoreInput;
    private RecyclerView mRlvSearchGuiderStore;
    @Inject
    SearchGuiderStorePresenter mSearchGuiderStorePresenter;
    private boolean ishavedefaultaddress = false;//是否有收货地址
    private LinearLayout mLlSearchGuiderNone;
    List<StoreInfo.Convenient> mGuiderStore = new ArrayList<>();
    private SearchGuiderStoreAdapter mAdapter;
    private AlertDialog mAlertDialog;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        DaggerMineComponent.builder().appComponent(App.getInstance().getAppComponent()).mineModule(new MineModule(this)).build().inject(this);
        setTbTitle("搜索门店");
        isEnable(false);
        setTbMenu(R.mipmap.icon_title_detail);
        initId();
        initData();
    }

    private void initId() {
        mEtSearchGuiderStoreInput = getViewById(R.id.et_search_guider_store_input);
        mRlvSearchGuiderStore = getViewById(R.id.rlv_search_guider_store);
        mLlSearchGuiderNone = getViewById(R.id.ll_search_guider_none);
    }

    private void initData() {
        mAdapter = new SearchGuiderStoreAdapter(mGuiderStore);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRlvSearchGuiderStore.setLayoutManager(manager);
        mRlvSearchGuiderStore.setAdapter(mAdapter);

        //绑定事件
        mAdapter.setOnClickButtonListener(new SearchGuiderStoreAdapter.OnClickButtonListener() {
            @Override
            public void clickBindOrUnbind(int position) {
                if (!ishavedefaultaddress) {
                    TipUtils.showToast(SearchGuiderStoreActivity.this, "请设置默认收货地址", 3);
                    return;
                }
                //弹出提示框
                LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(SearchGuiderStoreActivity.this).inflate(R.layout.pop_shopperssearch, null);

                TextView  tvCancel = (TextView) linearLayout.findViewById(R.id.tv_pop_search_cancel);
                TextView tvSubimt = (TextView) linearLayout.findViewById(R.id.tv_pop_search_submit);
                AlertDialog.Builder builder = new AlertDialog.Builder(SearchGuiderStoreActivity.this).setView(linearLayout);
                mAlertDialog = builder.show();

                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAlertDialog.dismiss();
                    }
                });

                tvSubimt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mAlertDialog.dismiss();
                        //发出绑定申请
                        mSearchGuiderStorePresenter.bindStoreGuider(mGuiderStore.get(position));
                    }
                });

            }
        });


        //搜索框中的事实搜索
        mEtSearchGuiderStoreInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    getDefualtStore();
                } else {
                    //根据字符串显示数据
                    getSearchStores(s.toString().trim());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //获取默认地址
        mSearchGuiderStorePresenter.getDefaultReceivingAdd();
        //获取默认的搜索门店
        getDefualtStore();
    }

    /**
     * 获取搜索的结果 乐
     *
     * @param
     */
    private void getSearchStores(String str) {
        mSearchGuiderStorePresenter.getSearchStores(str);
    }

    private void getDefualtStore() {
        mSearchGuiderStorePresenter.getDefualtStore();
    }

    @Override
    protected int setView() {
        return R.layout.activity_search_guider_store;
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    protected void clickMenu() {
        super.clickMenu();
        Intent intent = new Intent(this, GuiderHistoryRequestActivity.class);
        startActivity(intent);
    }

    @Override
    public void showError(String msg) {
        TipUtils.showToast(App.getInstance(), msg, 2);
    }

    @Override
    public void getDefaultReceivingAddSucc(boolean b) {
        ishavedefaultaddress = b;
    }

    @Override
    public void showEmpty() {
        mLlSearchGuiderNone.setVisibility(View.VISIBLE);
        mRlvSearchGuiderStore.setVisibility(View.GONE);

    }

    @Override
    public void getDefaultStoreSucc(List<StoreInfo.Convenient> list) {
        mGuiderStore.clear();
        mGuiderStore = list;
        mAdapter.setNewData(list);
    }

    @Override
    public void bindGuiderSucc() {
        TipUtils.showToast(App.getInstance(), "绑定成功，请等待审核", 1);
    }

    /**
     * 拿到搜索结果 显示数据 乐
     *
     * @param convenientList
     */
    @Override
    public void getSearchStoresSucc(List<StoreInfo.Convenient> convenientList) {
        mGuiderStore.clear();
        mGuiderStore.addAll(convenientList);
        mAdapter.setNewData(convenientList);
    }

}
